package com.sj.purchase.modules.cost.web;

import com.sj.purchase.common.Result;
import com.sj.purchase.modules.cost.domain.SucTask;
import com.sj.purchase.modules.cost.service.SucTaskService;
import com.sj.purchase.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import static com.sj.purchase.common.ResultGenerator.ok;

/**
 * 同步差异
 * Created by MaJinGe on 17-11-09.
 */
@RestController
@RequestMapping("api/sucTask")
public class SucTaskController {

    @Autowired
    private SucTaskService sucTaskService;

    /**
     * 查询当天未同步的全部数据
     * @return
     */
    @GetMapping("/findAll")
    public List<SucTask> findAllByDate(Date start, Date end) {
        return sucTaskService.findByDate(start,end);
    }

    /**
     * 根据ID同步未同步的数据
     * @param
     * @return
     */
    @GetMapping("/task")
    public Result<String> executeTask(String type, String date) {
        SucTask.TaskType[] taskTypes = SucTask.TaskType.values();
        for (SucTask.TaskType taskType:taskTypes){
            if (taskType.getName().equals(type)){
                try {
                    Date day = DateUtils.parseDate(date,"yyyy-MM-dd");
                    sucTaskService.syncData(taskType,day);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        return ok();
    }

//    @GetMapping("/task")
//    public Result<String> executeTask(SucTask.TaskType type,Date date){
//
//        System.out.println("type值:"+type);
//        System.out.println("date值:"+date);
//
//        return ok();
//    }

}
